package com.chinacoal.microservice.service.impl;

import com.alibaba.fastjson.JSON;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.chinacoal.microservice.mapper.CcmsSmartleasingRoomMapper;
import com.chinacoal.microservice.mapper.CcmsSmartleasingTaxRateTemplateMapper;
import com.chinacoal.microservice.pojo.CcmsSmartleasingRoom;
import com.chinacoal.microservice.pojo.CcmsSmartleasingTaxRateTemplate;
import com.chinacoal.microservice.pojo.Currency;
import com.chinacoal.microservice.service.FeeRateTemplateService;
import com.chinacoal.microservice.util.CodeMsg;
import com.chinacoal.microservice.util.QueryCriteria;
import com.chinacoal.microservice.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author zhuliang
 * @version V1.0
 * @Description: 费用税率模板-后端实现类
 * @date 2020-12-05 15：03
 */
@Slf4j
@Service
public class FeeRateTemplateServiceImpl implements FeeRateTemplateService {

    @Autowired
    private CcmsSmartleasingTaxRateTemplateMapper taxRateTemplateMapper;

    @Autowired
    private CcmsSmartleasingRoomMapper roomMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @Description: 查询所有的费用税率模板信息(带分页)
     * @author zhuliang
     * @date 2020/12/5 15:21
     */
    @Override
    public Result<List<CcmsSmartleasingTaxRateTemplate>> getAllByPage(QueryCriteria queryCriteria) {
        //设置分页条件
        Page<CcmsSmartleasingTaxRateTemplate> toPage = new Page<>(queryCriteria.getPage(), queryCriteria.getPageSize());

        //开始分页查询
        List<CcmsSmartleasingTaxRateTemplate> taxRateTemplates = taxRateTemplateMapper.selectPage(toPage, new EntityWrapper<CcmsSmartleasingTaxRateTemplate>().eq("delete_sign", 0).orderBy("last_update_date", false));

        return Result.success(taxRateTemplates, toPage.getTotal());
    }

    /**
     * @Description: 条件查询所有的费用税率模板信息(带分页)
     * @author zhuliang
     * @date 2020/12/5 15:29
     */
    @Override
    public Result<List<CcmsSmartleasingTaxRateTemplate>> getAllByCondition(QueryCriteria queryCriteria) {
        //设置分页条件
        Page<CcmsSmartleasingTaxRateTemplate> toPage = new Page<>(queryCriteria.getPage(), queryCriteria.getPageSize());

        //设置查询条件
        EntityWrapper<CcmsSmartleasingTaxRateTemplate> wrapper = new EntityWrapper<>();
        wrapper.eq("delete_sign", 0)
                .like("tax_rate_template_name", queryCriteria.getInputString())
                .orderBy("last_update_date", false);

        //开始分页查询
        List<CcmsSmartleasingTaxRateTemplate> taxRateTemplates = taxRateTemplateMapper.selectPage(toPage, wrapper);

        return Result.success(taxRateTemplates, toPage.getTotal());
    }

    /**
     * @Description: 查询相关房间信息
     * @author zhuliang
     * @date 2020/12/5 16:03
     */
    @Override
    public Result<List<CcmsSmartleasingRoom>> echoAssociatedRoom(String pkTaxRateTemplateId) {
        //根据税率模板查询相关房间信息
        List<CcmsSmartleasingRoom> roomByFeeRateTemplateId = roomMapper.getRoomByFeeRateTemplateId(pkTaxRateTemplateId);

        return Result.success(roomByFeeRateTemplateId);
    }

    /**
     * @Description: 添加费用税率模板消息
     * @author zhuliang
     * @date 2020/12/5 16:18
     */
    @Override
    public Result addFeeRateTemplate(CcmsSmartleasingTaxRateTemplate taxRateTemplate, String token) {
        //得到登录人id
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //完善信息
        taxRateTemplate.setCreatedBy(currency.getLoginId());
        taxRateTemplate.setCreationDate(new Date());
        taxRateTemplate.setLastUpdatedBy(currency.getLoginId());
        taxRateTemplate.setLastUpdateDate(new Date());
        taxRateTemplate.setPkTaxRateTemplateId(UUID.randomUUID() + "");

        //添加税率模板消息
        taxRateTemplateMapper.insert(taxRateTemplate);

        return Result.success();
    }

    /**
     * @Description: 根据税率模板id查询模板消息
     * @author zhuliang
     * @date 2020/12/5 16:24
     */
    @Override
    public Result<CcmsSmartleasingTaxRateTemplate> getTemplateById(String pkTaxRateTemplateId) {
        //根据id查询模板消息
        CcmsSmartleasingTaxRateTemplate taxRateTemplate = taxRateTemplateMapper.selectById(pkTaxRateTemplateId);

        //查询是否有房间与之关联
        String templateId = roomMapper.getByFeeRateTemplateId(pkTaxRateTemplateId);
        if (StringUtils.isNotBlank(templateId)) {
            return Result.success(new CodeMsg(30000, "当前有房间正在使用该模板，如修改税率则相关房间收费规则将在下月按最新规则执行收费，是否继续"), taxRateTemplate);
        }

        return Result.success(taxRateTemplate);
    }

    /**
     * @Description: 修改费用税率模板消息
     * @author zhuliang
     * @date 2020/12/5 16:34
     */
    @Override
    public Result updateTemplate(CcmsSmartleasingTaxRateTemplate taxRateTemplate, String token) {
        //得到登录人id
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //完善信息
        taxRateTemplate.setLastUpdateDate(new Date());
        taxRateTemplate.setLastUpdatedBy(currency.getLoginId());

        //修改模板消息
        taxRateTemplateMapper.updateById(taxRateTemplate);

        return Result.success();
    }

    /**
     * @Description: 根据税率模板id删除模板消息
     * @author zhuliang
     * @date 2020/12/5 16:51
     */
    @Override
    public Result deleteTemplate(String pkTaxRateTemplateId, String token) {
        //得到登录人id
        Currency currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getSignature());
        if (null == currency) {
            currency = (Currency) redisTemplate.opsForValue().get("ccms:" + JWT.decode(token).getClaim("phone").asString());
        }

        //查询是否有房间与之关联
        String templateId = roomMapper.getByFeeRateTemplateId(pkTaxRateTemplateId);
        if (StringUtils.isNotBlank(templateId)) {
            log.error(CodeMsg.ROOM_USING_TEMPLATE.toString() + "-->还要房间在使用该模板:" + pkTaxRateTemplateId);
            return Result.error(CodeMsg.ROOM_USING_TEMPLATE);
        }

        //根据id删除
        taxRateTemplateMapper.updateByDeleteId(pkTaxRateTemplateId, new Byte("1"), new Date(), currency.getLoginId());

        return Result.success();
    }
}
